package com.jsh.mypor.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jsh.mypor.pojo.Teacher;
import com.jsh.mypor.service.TeacherService;
import com.jsh.mypor.util.MD5;
import com.jsh.mypor.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/sms/teacherController")
public class TeacherController {
    @Autowired
    private TeacherService teacherService;

    //http://localhost:9001/sms/teacherController/getTeachers/1/3
    @GetMapping("/getTeachers/{pageNo}/{pageSize}")
    public Result getTeacher(
            @PathVariable Integer pageNo,
            @PathVariable Integer pageSize,
            Teacher teacher
    ){
        Page<Teacher> pageParam=new Page<>(pageNo,pageSize);
        IPage<Teacher> iPage=teacherService.getTeacherByOpr(pageParam,teacher);
        return Result.ok(iPage);

    }
    @PostMapping("/saveOrUpdateTeacher")
    public Result saveOrUpdateTeacher(
            @RequestBody Teacher teacher
    ){
        Integer id = teacher.getId();
        if (id==null||id==0){
            teacher.setPassword(MD5.encrypt(teacher.getPassword()));
        }
        boolean b = teacherService.saveOrUpdate(teacher);
        return Result.ok();
    }
    @DeleteMapping("/deleteTeacher")
    public Result deleteTeacher(
           @RequestBody List<Integer> ids
    ){

        boolean b = teacherService.removeByIds(ids);
        return Result.ok();
    }
}
